#!/bin/bash

set -euo pipefail

vault_name=""
source="https://gitee.com/Charlotto_z/linux-conf/raw/master/obsidian/obsidian-vault-conf.tar"

# empty para pass check
if [[ $# -eq 1 ]]; then
    vault_name=$1;
else
    echo "Usage: obsidian-mkvault.sh VALUE_NAME";
    exit 1;
fi

# file already exist check
if [[ ! -e $vault_name ]]; then
    mkdir $vault_name && cd $vault_name;
else 
    echo -e "ERROR: vault already exists.";
    exit 1;
fi

if ! curl -f $source | tar -x; then
    cd .. && rm -r $vault_name;
    echo -e "ERROR: failed to get vault config.\nERROR: vault '$vault_name' has been deleted.";
    exit 1;
fi
